package com.shadow.web.back;

import com.shadow.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shadow.domain.User;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController extends BaseController{
    @Autowired
    private LoginService loginService;

    @RequestMapping(value="/user/login",method=RequestMethod.GET)
    public ModelAndView login(ModelAndView modelAndView) {

        modelAndView.addObject("user", "shadow");
        modelAndView.setViewName("MyHome");
        return modelAndView;
    }



    @RequestMapping(value="/user/login",method=RequestMethod.POST)
    public String logon(String userName,String passWord){
        User user = null;
        try {
            user = loginService.getUser(userName, passWord);
        } catch (RuntimeException e) {
        }
        if(user!=null){
            return "MyHome";
        }else{
            return "login";
        }
    }

    @RequestMapping(value="/user/register",method=RequestMethod.POST)
    public String register(User user){
        loginService.addUser(user);
        return "login";
    }

    @RequestMapping(value="/user/toRegister",method=RequestMethod.GET)
    public String toRegister(){
        return "register";
    }
}